package com.dingding.order.core.syncorder.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dao.impl.BaseDaoImpl;
import com.dingding.common.core.beans.SyncOrder;
import com.dingding.common.core.util.paravalidate.ValidateUtil;
import com.dingding.order.core.syncorder.dao.ISyncOrderDao;

/**
 * 
 * @ClassName: ISyncOrderDao 
 * @Description: 同步订单DAO 
 * @author CHEHAUNBO 
 * @date 2016年7月26日 下午12:42:37
 * 
 */
@Component("syncOrderDao")
public class SyncOrderDaoImpl extends BaseDaoImpl<SyncOrder> implements ISyncOrderDao {

	private Logger log = Logger.getLogger(SyncOrderDaoImpl.class);

	/**
	 * 
	 * 获取所有的待同步订单
	 * 
	 * @Title: getAllSyncOrder 
	 * @return  List<SyncOrder> 所有待同步的集合
	 * @author CHEHAUNBO
	 * 
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public List<SyncOrder> getAllSyncOrder() {

		List<SyncOrder> syncOrders = new ArrayList<SyncOrder>();
		
		//查询所有未被发的单
		Criterion criterion = Restrictions.eq("isPublished", 0);
		try {
			syncOrders = super.queryCriteriaList(criterion);
		} catch (Exception e) {
			log.error("查询所有待同步的订单出现异常！", e);
		}
		
		//验证所有待同步订单是否可用
		for(SyncOrder order : syncOrders){
			if( !ValidateUtil.validateBean(order, SyncOrder.class) ){
				order.setIsPublished(2);//设置为订单不可用
				super.update(order);
				syncOrders.remove(order);
			}
		}

		return syncOrders;
	}

}
